<template>
  <div class="modal">
    <!-- 遮罩 -->
    <div class="mask" @click="isShowLoginModalFn(false)"></div>
    <!-- 登录框 -->
    <div class="loginbox">
      <!-- 关闭按钮 -->
      <div class="close" @click="isShowLoginModalFn(false)"></div>
      <!-- 导航 -->
      <nav class="navs">
        <a
          href="javascript:;"
          :class="{ active: isShowPhoneLogin }"
          @click="isShowPhoneLogin = true"
          >手机号码登录</a
        >
        |
        <a
          href="javascript:;"
          @click="isShowPhoneLogin = false"
          :class="{ active: !isShowPhoneLogin }"
          >微信扫码登录</a
        >
      </nav>
      <!-- 导航对应内容 -->
      <div class="content">
        <div v-show="isShowPhoneLogin" class="PhoneLogin">
          <!-- 手机号 -->
          <div class="row">
            <input
              type="text"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <!-- 滑动条 -->
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="imgs"
            ></slide-verify>
          </div>
          <!-- 验证码 -->
          <div class="row">
            <input
              type="text"
              placeholder="请输入短信验证码"
              v-model="yzmCode"
            />
            <button class="btn btnCode" @click="getCode" v-show="!isShowCount">
              获取验证码
            </button>
            <button class="btn btnCode" v-show="isShowCount">
              {{ count }}秒
            </button>
          </div>
          <!-- 登录按钮 -->
          <div class="row">
            <button class="btn btnLogin" @click="LoginFn">登录</button>
          </div>
        </div>
        <div v-show="!isShowPhoneLogin">微信</div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapActions } from "vuex";
import { sendSMSapi, phoneReginAPI } from "@/requeset/api.js";
import { validateTelephoneNumber } from "@/utils";

export default {
  data() {
    return {
      isShowPhoneLogin: true,
      msg: "向右滑动",
      // 滑块的背景图
      imgs: [
        require("@/assets/img/integral-01.9386d4bf.png"),
        require("@/assets/img/integral-02.150d92a1.png"),
        require("@/assets/img/integral-03.9870f3f1.png"),
        require("@/assets/img/integral-04.afadcbdf.png"),
      ],
      // 手机号码
      phoneNumber: "",
      // 倒计时
      count: 5,
      // 控制显示 倒计时和 文字
      isShowCount: false,
      // 验证码
      yzmCode: "",
    };
  },
  methods: {
    ...mapMutations({
      isShowLoginModalFn: "isShowMoadl/isShowLoginModalFn",
      changeIsLogined: "LoginStatu/changeIsLogined",
      // 改变提示组件状态
      changeIsShowToast: "ShowToast/changeIsShowToast",
    }),
    ...mapActions({
      asyncToast: "ShowToast/asyncToast",
    }),
    //----------
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },

    // 封装验证手机号和滑动
    toVerify() {
      // 判断手机
      if (!validateTelephoneNumber(this.phoneNumber)) {
        // 控制提示组件并传值
        this.asyncToast({
          showToast: true,
          title: "请输入正确的手机号码！！！！",
          type: "danger",
        });
        return;
      }
      // 判断滑动
      if (this.msg == "向右滑动" || this.msg == "再试一次") {
        alert("请重新滑动滑块验证码");
        return;
      }
      return true;
    },

    // 获取验证码
    async getCode() {
      // 判断手机和滑块验证码
      let req = this.toVerify();
      if (!req) return;
      //   发送请求，倒计时
      let res = await sendSMSapi({
        phone: this.phoneNumber,
      });
      console.log("短信", res);
      if (!res) return;
      // console.log("出错了不应该往下走");
      //   倒计时
      this.isShowCount = true;
      let timer = setInterval(() => {
        this.count--;
        if (this.count == 0) {
          clearInterval(timer);
          this.count = 5;
          this.isShowCount = false;
        }
      }, 1000);
    },

    // 登录按钮
    async LoginFn() {
      // 1.判断手机号码和滑动
      let req = this.toVerify();
      if (!req) return;
      // 2.判断验证码非空
      if (!this.yzmCode) {
        alert("请输入验证码!");
        return;
      }
      // 3.发送请求
      let res_p = await phoneReginAPI({
        verifyCode: this.yzmCode,
        phone: this.phoneNumber,
      });
      console.log("请求结果", res_p);
      if (!res_p) return;
      if (res_p.code == 0) {
        alert("登录成功");
        // 存储token
        localStorage.setItem("x-auth-token", res_p["x-auth-token"]);
        // 隐藏登录框
        this.isShowLoginModalFn(false);
        // 显示购物车 隐藏登录按钮
        this.changeIsLogined(true);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.modal {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .mask {
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .loginbox {
    background: url("@/assets/img/login-box-bg.png");
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    .close {
      background: url("@/assets/img/close.png");
      width: 22px;
      height: 22px;
      position: absolute;
      right: 60px;
      top: 10px;
      cursor: pointer;
    }
    .navs {
      margin-top: 50px;
      text-align: center;
      font-size: 18px;
      a {
        text-decoration: none;
        color: black;
        &.active {
          color: blue;
        }
      }
    }
    .content {
      width: 355px;
      margin: 0 auto;
      margin-top: 30px;
      .PhoneLogin {
        .row {
          display: flex;
          margin-bottom: 15px;
          input {
            flex: 1;
            height: 40px;
            text-indent: 2em;
            outline: none;
          }
          .btn {
            color: #fff;
            background-color: #09328c;
            border: 0;
            cursor: pointer;
          }
          .btnCode {
            margin-left: 10px;
            width: 80px;
          }
          .btnLogin {
            flex: 1;
            height: 40px;
          }
        }
      }
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}
</style>